﻿//namespace EasyToolkit.Excel.BindingTemplates.Contextual
//{
//    using System;
//    using System.Collections.Generic;
//    using EasyToolkit.BindingTemplates.Binding;
//    using EasyToolkit.Tools.Reflection;
//    using Microsoft.Office.Interop.Excel;
//    using EasyToolkit.BindingTemplate;
    
//    public class ContextualPropertyDefinition
//    {
//        IBindingDefinition bindingDefinition;
//        ContextualPropertyColor defaultContextualPropertyColor;
//        List<ContextualPropertyColor> contextualPropertyColors;

//        #region .ctors
//        private ContextualPropertyDefinition() 
//        { }
//        #endregion

//        #region public methods
//        public void ResolveBinding(Range concernedRange, object dataSource, string bindingName)//, ContextualPropertyColor contextContextualPropertyColor)
//        {
//            object[] parameters = new object[] { dataSource, bindingName };
//            Comment comment = concernedRange.Comment;
//            if (comment != null)
//                comment.Delete();

//            ContextualProperty contextualProperty = bindingDefinition.MethodInfo.Invoke(bindingDefinition.MethodInfo.IsStatic ? null : dataSource, parameters) as ContextualProperty;
//            Interior interior = concernedRange.Interior;
//            Font font = concernedRange.Font;
//            if (contextualProperty != null)
//            {
//                if (!string.IsNullOrEmpty(contextualProperty.Comment))
//                    concernedRange.AddComment(contextualProperty.Comment);

//                if (contextualProperty.StyleKey.HasValue)
//                {
//                    ContextualPropertyColor contextualPropertyColor = contextualPropertyColors[contextualProperty.StyleKey.Value];
//                    interior.Color = contextualPropertyColor.BackgroundColor;
//                    font.Color = contextualPropertyColor.FontColor;
//                }
//                else if (defaultContextualPropertyColor != null)
//                {
//                    interior.Color = defaultContextualPropertyColor.BackgroundColor;
//                    font.Color = defaultContextualPropertyColor.FontColor;
//                }
//            }
//            else if (defaultContextualPropertyColor != null)
//            {
//                interior.Color = defaultContextualPropertyColor.BackgroundColor;
//                font.Color = defaultContextualPropertyColor.FontColor;
//            }
//        }
//        #endregion

//        #region static methods
//        public static ContextualPropertyDefinition CreateInstance(Type type, Range contextualPropertyRange, Range defaultStyleRange)
//        {
//            ContextualPropertyDefinition definition = null;
//            string contextualPropertyValue = contextualPropertyRange.Value2 as string;
//            try
//            {
//                string[] values = contextualPropertyValue.Split('|');
//                if (!string.IsNullOrEmpty(values[0]))
//                {
//                    List<ContextualPropertyColor> contextualPropertyColors = null;
//                    BindingDefinitionMethod bindingDefinition = CreateBindingDefinition(type, values[0]) as BindingDefinitionMethod;

//                    if (values.Length > 1 && !string.IsNullOrEmpty(values[1]))
//                    {
//                        string contextualPropertyDefinition = values[1];
//                        string[] pathParts = contextualPropertyDefinition.Split('%');
//                        string namedRange;
//                        Worksheet namedRangeSheetOwner;

//                        if (pathParts.Length == 1)
//                        {
//                            namedRange = pathParts[0];
//                            namedRangeSheetOwner = contextualPropertyRange.Worksheet;
//                        }
//                        else
//                        {
//                            Application app = contextualPropertyRange.Application;
//                            Workbook workbook = app.ActiveWorkbook;
//                            namedRangeSheetOwner = workbook.GetWorksheet(pathParts[0]);
//                            namedRange = pathParts[1];
//                            if (namedRangeSheetOwner == null)
//                                throw new BindingTemplateException(string.Format("Create Can't find the worksheet '{0}'.", pathParts[1]));
//                        }
//                        contextualPropertyColors = GetContextualPropertyColors(namedRangeSheetOwner, namedRange);
//                        definition = new ContextualPropertyDefinition();
//                        definition.bindingDefinition = bindingDefinition;
//                        definition.contextualPropertyColors = contextualPropertyColors;

//                        if (defaultStyleRange != null)
//                        {
//                            definition.defaultContextualPropertyColor = new ContextualPropertyColor();
//                            Interior interior = defaultStyleRange.Interior;
//                            Font font = defaultStyleRange.Font;
//                            definition.defaultContextualPropertyColor.BackgroundColor = (double)interior.Color;
//                            definition.defaultContextualPropertyColor.FontColor = (double)font.Color;
//                        }
//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                string message = string.Format("Create 'ContextualPropertyDefinition' '{0}' failed. {1}", contextualPropertyValue, ex.Message);
//                throw new BindingTemplateException(message, ex);
//            }
//            return definition;
//        }

//        private static IBindingDefinition CreateBindingDefinition(Type type, string bindingName)
//        {
//            string[] values = bindingName.Split('%');
//            if (values.Length != 1 && values.Length != 3)
//                throw new BindingTemplateException(string.Format("The binding property '{0}' is not correct.", bindingName));

//            string methodName;
//            Type typeToUse;
//            if (values.Length == 1)
//            {
//                methodName = values[0];
//                typeToUse = type;
//            }
//            else
//            {
//                string assemblyName = values[0];
//                string typeName = values[1];
//                methodName = values[2];
//                typeToUse = TypeHelpers.GetType(assemblyName, typeName);
//                if (typeToUse == null)
//                    throw new BindingTemplateException(string.Format("Can't find the class '{0}' in assembly '{1}.", typeName, assemblyName));
//            }
//            IBindingDefinition bindingDefinition = BindingDefinition.CreateInstance(typeToUse, methodName, true, null);
//            if (bindingDefinition == null)
//                throw new BindingTemplateException(string.Format("The binding property '{0}' must be a method.", bindingName));
//            return bindingDefinition;
//        }

//        private static List<ContextualPropertyColor> GetContextualPropertyColors(Worksheet namedRangeSheetOwner, string namedRange)
//        {
//            try
//            {
//                List<ContextualPropertyColor> contextualPropertyColors = new List<ContextualPropertyColor>();
//                Range colorsRange = namedRangeSheetOwner.get_Range(namedRange, Type.Missing);
//                Range rows = colorsRange.Rows;
//                for (int cpt = 1; cpt <= rows.Count; cpt++)
//                {
//                    Range range = colorsRange.Cells[cpt, 1];
//                    Interior interior = range.Interior;
//                    Font font = range.Font;
//                    ContextualPropertyColor contextualPropertyColor = new ContextualPropertyColor()
//                    {
//                        BackgroundColor = (double)interior.Color,
//                        FontColor = (double)font.Color
//                    };
//                    contextualPropertyColors.Add(contextualPropertyColor);
//                }
//                return contextualPropertyColors;
//            }
//            catch (Exception ex)
//            {
//                throw new BindingScenarioException(string.Format("Can't find named range '{0}' in the worksheet '{1}'.", namedRangeSheetOwner.Name, namedRange), ex);
//            }

//        }
//        #endregion
//    }
//}
